//
//  YFMMDrawerStateManager.m
//  SquashSpark
//
//  Created by Coollang on 2017/10/26.
//  Copyright © 2017年 Coollang-YF. All rights reserved.
//

#import "YFMMDrawerStateManager.h"
#import "YFMenuViewController.h"
#import "YFSportViewController.h"
#import "MyNavigationViewController.h"
#import <MMDrawerController/MMDrawerVisualState.h>

NSString * const kChangeCenterViewControllerNoti = @"ChangeCenterViewControllerNoti";

@interface YFMMDrawerStateManager()

@property (nonatomic, strong) UIView *maskView;

@end

@implementation YFMMDrawerStateManager

+ (instancetype)sharedManager {
    static YFMMDrawerStateManager *_sharedManager = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _sharedManager = [[YFMMDrawerStateManager alloc] init];
        [_sharedManager setLeftDrawerAnimationType:MMDrawerAnimationTypeSlideAndScale];
        [_sharedManager setRightDrawerAnimationType:MMDrawerAnimationTypeSlideAndScale];
    });
    return _sharedManager;
}

- (MMDrawerController *)FetchDrawerController {
    if (_drawerController == nil) {
        YFMenuViewController *leftVc = [[YFMenuViewController alloc] initWithNibName:@"YFMenuViewController" bundle:nil];
        _menuController = leftVc;
        MyNavigationViewController *centerNav = (MyNavigationViewController *)[leftVc.centerVcCache objectForKey:@"Training"];
        MMDrawerController *drawerController = [[MMDrawerController alloc] initWithCenterViewController:centerNav leftDrawerViewController:leftVc];
        [drawerController setRestorationIdentifier:@"MMDrawer"];
        [drawerController setShowsShadow:YES];
        [drawerController setShadowColor:kSeparationLineColor];
        [drawerController setShadowOpacity:0.8];
        
        [drawerController setStatusBarViewBackgroundColor:kMaskViewBgColor];
        
        CGFloat maxLeftWidth = kYFScreenWidth - kAutoWid(60);
        [drawerController setMaximumLeftDrawerWidth:maxLeftWidth];
        [drawerController setOpenDrawerGestureModeMask:MMOpenDrawerGestureModePanningNavigationBar | MMOpenDrawerGestureModeBezelPanningCenterView];
        [drawerController setCloseDrawerGestureModeMask:MMCloseDrawerGestureModeAll];
        drawerController.centerHiddenInteractionMode = MMDrawerOpenCenterInteractionModeFull;
        [drawerController
         setDrawerVisualStateBlock:^(MMDrawerController *drawerController, MMDrawerSide drawerSide, CGFloat percentVisible) {
             //YFLog(@"%f",percentVisible);
             // 添加蒙版
             UIViewController *vc = drawerController.centerViewController;
             if ([vc isKindOfClass:[MyNavigationViewController class]]) {
                 MyNavigationViewController *navVc = (MyNavigationViewController *)vc;
                 navVc.maskView.alpha = percentVisible;
                 navVc.maskView.hidden = percentVisible < 0.1;
             }
             if (drawerSide == MMDrawerSideLeft) {
                 self.leftControllerPercentVisible = percentVisible;
             }else if (drawerSide == MMDrawerSideRight){
                 self.rightControllerPercentVisible = percentVisible;
             }
             MMDrawerControllerDrawerVisualStateBlock block;
             block = [[YFMMDrawerStateManager sharedManager]
                      drawerVisualStateBlockForDrawerSide:drawerSide];
             if(block){
                 block(drawerController, drawerSide, percentVisible);
             }
         }];
        
        _drawerController = drawerController;
    }
    return _drawerController;
}



/*下面为拷贝示例代码，实现击中特殊效果，放在这里备用*/
-(MMDrawerControllerDrawerVisualStateBlock)drawerVisualStateBlockForDrawerSide:(MMDrawerSide)drawerSide {
    MMDrawerAnimationType animationType;
    if(drawerSide == MMDrawerSideLeft){
        animationType = self.leftDrawerAnimationType;
    }
    else {
        animationType = self.rightDrawerAnimationType;
    }
    
    MMDrawerControllerDrawerVisualStateBlock visualStateBlock = nil;
    switch (animationType) {
        case MMDrawerAnimationTypeSlide:
            visualStateBlock = [MMDrawerVisualState slideVisualStateBlock];
            break;
        case MMDrawerAnimationTypeSlideAndScale:
            visualStateBlock = [MMDrawerVisualState slideAndScaleVisualStateBlock];
            break;
        case MMDrawerAnimationTypeParallax:
            visualStateBlock = [MMDrawerVisualState parallaxVisualStateBlockWithParallaxFactor:2.0];
            break;
        case MMDrawerAnimationTypeSwingingDoor:
            visualStateBlock = [MMDrawerVisualState swingingDoorVisualStateBlock];
            break;
        default:
            visualStateBlock =  ^(MMDrawerController * drawerController, MMDrawerSide drawerSide, CGFloat percentVisible){
                
                UIViewController * sideDrawerViewController;
                CATransform3D transform;
                CGFloat maxDrawerWidth = 0.0;
                
                if(drawerSide == MMDrawerSideLeft){
                    sideDrawerViewController = drawerController.leftDrawerViewController;
                    maxDrawerWidth = drawerController.maximumLeftDrawerWidth;
                }
                else if(drawerSide == MMDrawerSideRight){
                    sideDrawerViewController = drawerController.rightDrawerViewController;
                    maxDrawerWidth = drawerController.maximumRightDrawerWidth;
                }
                
                if(percentVisible > 1.0){
                    transform = CATransform3DMakeScale(percentVisible, 1.f, 1.f);
                    if(drawerSide == MMDrawerSideLeft){
                        transform = CATransform3DTranslate(transform, maxDrawerWidth*(percentVisible-1.f)/2, 0.f, 0.f);
                    }else if(drawerSide == MMDrawerSideRight){
                        transform = CATransform3DTranslate(transform, -maxDrawerWidth*(percentVisible-1.f)/2, 0.f, 0.f);
                    }
                }
                else {
                    transform = CATransform3DIdentity;
                }
                [sideDrawerViewController.view.layer setTransform:transform];
            };
            break;
    }
    return visualStateBlock;
}

@end
